package com.mobile.common.base.support;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

@SuppressWarnings("rawtypes")
public class XmlSupport {
	
	public XmlSupport() {}
	public XmlSupport(InputStream inputStream) throws DocumentException {
		reader = new SAXReader();
		reader.setEncoding("utf-8");
		doc = reader.read(inputStream);
	}

	private Document doc;
	private SAXReader reader;
	/**
	 * dom4j 
	 * @param node 父节点
	 * @param nodeKey	父节点的子path:/subpath
	 * @return
	 */
	public String getNodeValue(Node node, String nodeKey){
		Document temp = node.getDocument();
		Node nd = temp.selectSingleNode(node.getPath()+nodeKey);
		return nd!=null?nd.getText():"";
	}
	/**
	 * @param node "/abc/def"
	 * @return
	 */
	public List<String> getValue(String node){
		List<String> str = new ArrayList<String>();
		List nodes = doc.selectNodes(node);
		if (!nodes.isEmpty()) {
			Iterator it = nodes.iterator();
			while (it.hasNext()) {
				String temp = ((Element) it.next()).getTextTrim();
				str.add(temp);
			}
		}
		return str;
	}
	//---------------------------------------------------
	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

	
}
